<?php
namespace Service\Controller;

use Common\Controller\HomebaseController;
use Label\Model\CategoryModel;

class IndexController extends HomebaseController {

    protected $sp_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size    = 20;
        $this->sp_model     = D("Service/ServiceProvider");
    }

    // 服务商列表
    public function index() {
        $areas = getArea(2);
        $this->assign('area', json_encode($areas));
        $this->_lists($areas);
        $this->display();
    }

    public function service_info()
    {
        if ($id = I("get.id")) {
            $sp_info = $this->sp_model
                ->where(['id' => $id, 'del_time' => 0])
                ->field('*, (select group_concat(areaname) from rx_area where find_in_set(id, rx_service_provider.city_ids)) as city_names, (select group_concat(name) from rx_category where find_in_set(id, rx_service_provider.categorys)) as category_names')
                ->find();
            $sp_info['city_names']      = array_filter(explode(',', $sp_info['city_names']));
            $sp_info['category_names']  = array_filter(explode(',', $sp_info['category_names']));
            if ($sp_info['company_scale'] == 0) {
                $sp_info['scale'] = "不明确";
            } elseif ($sp_info['company_scale'] <= 20) {
                $sp_info['scale'] = "1~20人";
            } elseif ($sp_info['company_scale'] <= 100 && $sp_info['company_scale'] > 20) {
                $sp_info['scale'] = "20~100人";
            } elseif ($sp_info['company_scale'] <= 500 && $sp_info['company_scale'] > 100) {
                $sp_info['scale'] = "100~500人";
            } elseif ($sp_info['company_scale'] <= 1000 && $sp_info['company_scale'] > 500) {
                $sp_info['scale'] = "500~1000人";
            } elseif ($sp_info['company_scale'] <= 5000 && $sp_info['company_scale'] > 1000) {
                $sp_info['scale'] = "1000~5000人";
            } else {
                $sp_info['scale'] = "5000人以上";
            }
            $sp_info['established_time'] = substr($sp_info['established_time'], 0, 7);

            if ($sp_info) {
                $sp_financing   = M('sp_financing')->where(['sp_id' => $id])->select();
                $sp_link        = M('sp_link')->where(['sp_id' => $id])->select();
                $sp_memeber     = M('sp_member')->where(['sp_id' => $id])->select();
                $sp_project     = M('sp_project')
                    ->where(['sp_id' => $id])
                    ->field('*, (select areaname from rx_area where id = rx_sp_project.city_id) as city_name')
                    ->select();
                foreach ($sp_project as $k => $v) {
                    $v['imgs'] = array_filter(explode(',', $v['imgs']));
                    $sp_project[$k] = $v;
                }

                $this->assign('sp_info', $sp_info);
                $this->assign('sp_financing', $sp_financing);
                $this->assign('sp_link', $sp_link);
                $this->assign('sp_member', $sp_memeber);
                $this->assign('sp_project', $sp_project);
                $this->display();exit;
            } else {
                $this->error("请求参数错误");
            }
        } else {
            $this->error("请求参数错误");
        }
    }

    /**
     * 服务商列表处理方法,根据不同条件显示不同的列表
     * @param array $where 查询条件
     */
    private function _lists($areas){

        $where = array('del_time' => 0, 'status' => 1);
        // 分类
        $category_id = I('request.category_id');
        if(!empty($category_id)){
            $where[] = "FIND_IN_SET(" . $category_id . ", categorys)";
        }
        //项目分类
        $categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_SERVICE, 'status' => 1])->select();
        $this->assign('categorys', $categorys);

        // 城市
        $city_id = I('request.city_id');
        if(!empty($city_id)){
            $all_city_id = getAreaParentPath($city_id, $areas);
            $where[] = "FIND_IN_SET(" . $city_id . ", city_ids)";

            $this->assign('path', json_encode($all_city_id));
        }

        // 缓存值
        $cache_key = "";
        if ($where) {
            $cache_key = md5(implode(',', $where));
        }

        $count  = $this->sp_model->where($where)->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $this->sp_model
            ->where($where)
            ->order('sort desc')
            ->limit($page->firstRow , $page->listRows)
            ->field('*, (select group_concat(areaname) from rx_area where find_in_set(id, rx_service_provider.city_ids)) as city_names, (select round from rx_sp_financing where sp_id = rx_service_provider.id order by id desc limit 1) as financing_round')
            ->cache("service_list_" . $cache_key . '_' . $page->firstRow . '_' . $page->listRows, C('CACHE_EXPIRE'))
            ->select();
        foreach ($lists as $k => $v) {
            if ($v['company_scale'] == 0) {
                $v['scale'] = "不明确";
            } elseif ($v['company_scale'] <= 20) {
                $v['scale'] = "1~20人";
            } elseif ($v['company_scale'] <= 100 && $v['company_scale'] > 20) {
                $v['scale'] = "20~100人";
            } elseif ($v['company_scale'] <= 500 && $v['company_scale'] > 100) {
                $v['scale'] = "100~500人";
            } elseif ($v['company_scale'] <= 1000 && $v['company_scale'] > 500) {
                $v['scale'] = "500~1000人";
            } elseif ($v['company_scale'] <= 5000 && $v['company_scale'] > 1000) {
                $v['scale'] = "1000~5000人";
            } else {
                $v['scale'] = "5000人以上";
            }
            $v['established_time'] = substr($v['established_time'], 0, 7);
            $lists[$k] = $v;
        }
        $this->assign("page", $page->show('default'));
        $this->assign("formget", array_merge($_GET,$_POST));
        $this->assign("lists", $lists);
        $this->assign('total_pages', ceil($count / $this->page_size));
    }
}
